/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import model.Container;
import model.Content;
import model.PlaylistRecord;
import view.contents.ContentBox;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class ContainerBox extends JPanel {

	private Container container;
	private ContentBox contentBox;

	public boolean hasValidContent() {
		if (this.contentBox == null || this.getComponents() == null || this.getComponents().length == 0) {
			return false;
		}
		return this.contentBox.isSuccessfullyInitialized();
	}

	public ContainerBox(Container c) {
		this.container = c;
		this.init();
	}

	private void init() {
		// this.container.registerChangeNotifier(this);
		this.setLayout(new BorderLayout());
		this.setOpaque(false);
		// this.setBackground(Helper.getRandomColor());
		setIgnoreRepaint(true);
		// System.out.println("Setting bounds " +
		// ContainerBox.this.container.getLeft() + " " +
		// ContainerBox.this.container.getTop() + " " +
		// ContainerBox.this.container.getWidth() + " " +
		// ContainerBox.this.container.getHeight());
		this.setBounds(this.container.getLeft(), this.container.getTop(), this.container.getWidth(), this.container.getHeight());
		// this.setBorder(BorderFactory.createLineBorder(Color.blue));
		// this.setBackground(Helper.getRandomColor());
		// System.out.println(this.getBackground()+"COLOR");
	}

	@Override
	public int hashCode() {
		return this.container.getId();
	}

	@Override
	public boolean equals(Object o) {
		return ((o instanceof ContainerBox) && ((ContainerBox) o).getId() == this.getId());
	}

	public int getId() {
		return this.container.getId();
	}

	public Container getContainer() {
		return this.container;
	}

	public void setContent(final Content c) {

		if (this.getComponentCount() > 0) {
			ContentBox contentBox = (ContentBox) this.getComponent(0);
			contentBox.transition(c);
			contentBox.cleanUp();
			this.removeAll();
		}
		contentBox = ContentBox.getContentBox(c, this.container.getWidth(), this.container.getHeight());
		if(contentBox!=null) {
			this.add(this.contentBox);
			this.revalidate();
			this.repaint();
			contentBox.initialize();
		}
		
	}

	public void setContent(ContentBox c) {
		if (this.getComponentCount() > 0) {
			ContentBox contentBox = (ContentBox) this.getComponent(0);
			contentBox.cleanUp();
			this.removeAll();
		}
		contentBox = c;
		this.add(this.contentBox);
		this.revalidate();
		this.repaint();
		contentBox.initialize();
	}

	public void clear() {
		if (this.getComponentCount() > 0) {
			ContentBox contentBox = (ContentBox) this.getComponent(0);
			contentBox.cleanUp();
			this.removeAll();
			this.revalidate();
			this.repaint();
		}

	}

	public ContentBox getContentBox() {
		return this.contentBox;
	}

	public static void main(String[] args) {
	}

	/*
	 * This function is called by The Subject of the Observer Pattern to update
	 * the content of the container box
	 */
	public void update(Object o) {
		PlaylistRecord rec = (PlaylistRecord) o;
		if (rec == null) {
			this.clear();
		} else {
			this.setContent(rec.getContent());
		}
	}
}
